﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TotalCrytography.Data;
using TotalCrytography.Protector;

namespace TotalCrytography
{
    public partial class ChangeInfoGUI : Form
    {
        private UserDTO userInfo;

        public ChangeInfoGUI()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            userInfo = MainGUI.GetCurrentUser();
            if (userInfo != null)
            {
                tbEmail.Text = userInfo.Email;
                tbName.Text = userInfo.Name;
                tbPhone.Text = userInfo.Phone;
                tbAddress.Text = userInfo.Address;
                dtpBirthday.Value = DateTime.Parse(userInfo.Birthday);
            }
            else
            {
                MessageBox.Show("You have not login yet!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                btnChange.Enabled = false;
                btnClear.Enabled = false;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tbPass_Leave(object sender, EventArgs e)
        {
            Check();
        }

        private void tbRePass_Leave(object sender, EventArgs e)
        {
            Check();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            Init();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (!lbPassErr.Visible && !lbRePassErr.Visible && !lbOldPassErr.Visible)
            {
                userInfo.Name = tbName.Text;
                userInfo.Phone = tbPhone.Text;
                userInfo.Address = tbAddress.Text;
                userInfo.Birthday = dtpBirthday.Value.ToShortDateString();
                if (tbPass.TextLength > 0 && tbOldPass.TextLength > 0)
                {
                    if (Hash_Algorithm.VerifyHashWithSalt(tbOldPass.Text, "SHA1", userInfo.HashPass))
                    {
                        userInfo.Salt = Hash_Algorithm.CreateSalt(4, 8);
                        userInfo.HashPass = Hash_Algorithm.CreateHashWithSalt(tbPass.Text, "SHA1", userInfo.Salt);
                        Symmetric_Provider symProvider = new Symmetric_Provider(tbOldPass.Text);
                        string privateKey = symProvider.DecryptString(userInfo.PrivateKey);
                        symProvider = new Symmetric_Provider(tbPass.Text);
                        userInfo.PrivateKey = symProvider.EncryptString(privateKey);
                        MainGUI.SetCurrentPass(tbPass.Text);
                    }
                    else
                        MessageBox.Show("Old password is incorrect!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
                UserBUS.Update(userInfo);
                MessageBox.Show("Change info successfully!", "TotalCrytography", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            else
                MessageBox.Show("Input data is not valid!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }

        private void tbPass_TextChanged(object sender, EventArgs e)
        {
            Check();
        }

        private void tbOldPass_TextChanged(object sender, EventArgs e)
        {
            Check();
        }

        private void Check()
        {
            if (tbPass.TextLength > 0 && tbOldPass.TextLength == 0)
                lbOldPassErr.Visible = true;
            else
                lbOldPassErr.Visible = false;

            if (tbPass.TextLength == 0 && tbOldPass.TextLength > 0)
                lbPassErr.Visible = true;
            else
                lbPassErr.Visible = false;

            if (tbPass.Text != tbRePass.Text)
                lbRePassErr.Visible = true;
            else
                lbRePassErr.Visible = false;

            if (tbPass.TextLength == 0 && tbOldPass.TextLength > 0)
                lbPassErr.Visible = true;
            else
                lbPassErr.Visible = false;
        }

        private void tbRePass_TextChanged(object sender, EventArgs e)
        {
            Check();
        }
    }
}
